package com.huel.service.impl;

import com.github.pagehelper.PageHelper;
import com.huel.dto.Page;
import com.huel.dto.PageParams;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.huel.mapper.EmpMapper;
import com.huel.domain.Emp;
import com.huel.service.EmpService;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService{

    @Resource
    private EmpMapper empMapper;

    @Override
    public int deleteByPrimaryKey(Integer empId) {
        if(null==empId||this.selectByPrimaryKey(empId)==null)
            throw new RuntimeException("待删除记录不存在!");
        return empMapper.deleteByPrimaryKey(empId);
    }


    @Override
    public int insertSelective(Emp record) {
        if(record.getEmpName()==null||record.getEmpName().trim()=="")
            throw  new RuntimeException("员工姓名不能为空!");
        if(record.getDeptId()==null)
            throw  new RuntimeException("请选择部门!");
        return empMapper.insertSelective(record);
    }

    @Override
    public Emp selectByPrimaryKey(Integer empId) {
        if(null==empId)
            throw new RuntimeException("请指定员工记录id!");
        Emp emp=empMapper.selectByPrimaryKey(empId);
        if(null==emp)
            throw new RuntimeException("待查询记录不存在!");
        return emp;
    }

    @Override
    public int updateByPrimaryKeySelective(Emp record) {
        if(null==record.getEmpId()||this.selectByPrimaryKey(record.getEmpId())==null)
            throw new RuntimeException("待更新记录不存在!");
        if(record.getEmpName()==null||record.getEmpName().trim()=="")
            throw  new RuntimeException("员工姓名不能为空!");
        if(record.getDeptId()==null)
            throw  new RuntimeException("请选择部门!");
        return empMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Page<Emp> getEmpPageListByParams(PageParams pageParams) {
        // 启动分页
        com.github.pagehelper.Page<Emp> startPage = PageHelper.startPage(pageParams.getPageNumber(), pageParams.getPageSize());
        // 执行查询 返回结果是一个list
        List<Emp> empList= empMapper.getEmpPageListByParams(pageParams);
        Page page=new Page();
        page.setPages(startPage.getPages());
        page.setTotal(startPage.getTotal());
        page.setList(startPage.getResult());
        return page;
    }


}
